package org.ocean.online.sys;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Date;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.wicket.markup.MarkupType;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.protocol.http.servlet.ServletWebRequest;
import org.ocean.online.auth.AuthManager;
import org.ocean.online.auth.ExpirePage;
import org.ocean.online.utils.CommonUtil;
import org.ocean.online.utils.RenderUtil;
import org.ocean.online.utils.UserUtil;

public class DispatchPage extends WebPage{

	private static final long serialVersionUID = -5178945117364094269L;

	private static ScriptEngineManager sm = new ScriptEngineManager();
	private static ScriptEngine engine = sm.getEngineByName("JavaScript");
	
	public DispatchPage(){
	}

	@Override
	public MarkupType getMarkupType() {
		MarkupType type = new MarkupType(".*","text");
		return type;
	}

	
	@SuppressWarnings("deprecation")
	@Override
	public void renderPage() {
		AuthManager auth = AuthManager.getInstance();
		if(auth.getExpireTime()!=0 && auth.getExpireTime()>System.currentTimeMillis()){
			this.setResponsePage(new ExpirePage());
			return;
		}
		if(preRender()==false){
			return;
		}
		if(StringUtils.isEmpty(UserUtil.getUsernameFromRequest(getRequest()))){
			String url ="admin/login.html";
			String result = "<script type='text/javascript'>window.location='"+url+"'</script>";
			this.getResponse().write(result.getBytes());
			return;
		}
		String fullPath = getFullPath();
		
		String ext = CommonUtil.getExtOfFile(fullPath);
		if("html".equals(ext.toLowerCase())){
			RenderUtil.renderView(getRequest(),this.getResponse(),fullPath);
			return;
		}
		if("action".equalsIgnoreCase(ext)){
			RenderUtil.renderAction(engine,getRequest(),getResponse(),fullPath);
			return;
		}
		if("gz".equalsIgnoreCase(ext) || "js".equalsIgnoreCase(ext)|| "css".equalsIgnoreCase(ext)
				|| "png".equalsIgnoreCase(ext) || "gif".equalsIgnoreCase(ext) || "jpg".equalsIgnoreCase(ext)){
			HttpServletRequest request = ((ServletWebRequest) getRequest()).getContainerRequest();
			HttpServletResponse httpResponse = (HttpServletResponse) getResponse().getContainerResponse();
			if("gz".equalsIgnoreCase(ext)){
				httpResponse.setHeader("Content-Encoding", "gzip");
			}
			if(fullPath.contains(".css")){
				httpResponse.setHeader("Content-Type", "text/css");
			}
			if(fullPath.contains(".js")){
				httpResponse.setHeader("Content-Type", "text/javascript");
			}
			File file = new File(fullPath);
			String lastTime = new Date(file.lastModified()).toGMTString();
			try{
				String sinceTime = new Date(request.getHeader("If-Modified-Since")).toGMTString();
				if(lastTime.equals(sinceTime)){
					httpResponse.setStatus(304);
					return;
				}
			}catch(Exception ex){
				
			}
			
			httpResponse.setDateHeader("Last-Modified",file.lastModified()); 
			httpResponse.setDateHeader("Expires",file.lastModified());  
		}
		try {
			FileInputStream fios = FileUtils.openInputStream(new File(fullPath));
			// set mime type
			int size=0;
			while((size = fios.available())>0){
				byte[] buffer = new byte[size];
				fios.read(buffer);
				this.getResponse().getOutputStream().write(buffer);	
			}
			
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}

	protected boolean preRender() {
		return true;
	}

	protected String getFullPath() {
		HttpServletRequest request = ((ServletWebRequest) getRequest()).getContainerRequest();
		return UserUtil.getFullFilePath(request);
	}

}
